/**
 * @author wijnand.schepens
 */
package org.jfonia.assets.fonts;

import java.awt.Font;
import java.awt.Graphics2D;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

/**
 * Static methods provide access to fughetta font glyphs
 */
public class Fughetta {

	public static String fontPath       = "Fughetta.ttf"; // "/../resources/Fughetta.ttf";
	public static String propertiesPath = "org/jfonia/assets/fonts/Fughetta";     // "/../resources/Fughetta";
	
	/*
	 * (g needed for glyph-bounds)
	 */
	public static void init(Graphics2D g) throws Exception
	{
		initFont();
		initProperties(g);
	}
	
	protected static void initFont() throws Exception
	{
        InputStream in = (new MusicFont()).getClass().getResourceAsStream(fontPath);
        font = Font.createFont(Font.TRUETYPE_FONT,in).deriveFont(8f);
        	// scaled such that noteheads have height==2 !! 
        in.close();
	}
	
	protected static void initProperties(Graphics2D g) throws Exception
	{
		props = ResourceBundle.getBundle(propertiesPath);
		
		//map = new HashMap<String, String>();
		glyphMap = new HashMap<String, TextGlyph>();
		
		Enumeration<String> e = props.getKeys();
		while (e.hasMoreElements())
		{
			String key = e.nextElement();
			String val = props.getString(key);
			int pointCode = Integer.parseInt(val,16);
			char[] chars = Character.toChars(pointCode);
			String code = new String(chars);
			//System.out.println(key);
			//map.put(key, code);
			glyphMap.put(key,new TextGlyph(g, font, code));
		}
		
		
	}

	public static Font getFont()
	{ 
		return font; 
	}
	
//	public static Map<String, String> getCodeMap()
//	{
//		return map;
//	}
	
	public static TextGlyph getGlyph(String key)
	{
		TextGlyph fg = glyphMap.get(key);
		if (fg == null)
			throw new RuntimeException("unknown glyph");
		return fg;
	}
	
//	public static Map<String, FontGlyph> getGlyphMap()
//	{
//		return glyphMap;
//	}
	
	private static Font font;
	private static ResourceBundle props;
//	private static Map<String, String> map;
	private static Map<String, TextGlyph> glyphMap;
}
